"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 拷贝数据
Case Name   : copy_option指定null值是\r或\n进行copy from/to
Create At   : 2022/7/11
Owner       : opentestcase002
Description :
    1.创建测试表并插入数据
    drop table if exists t_copy_133;
    create table t_copy_133
    (sk integer,id varchar(16),name varchar(20),sq_ft integer);
    insert into t_copy_133 values (001,'sk1','tt1','');
    insert into t_copy_133 values (002,'sk2','tt2','');
    insert into t_copy_133 values (003,'sk3','tt3','');
    2.构造数据文件
    touch /xx/testcopy133.dat
    3.null值是\r进行copy to
    copy t_copy_133 to '/xx/testcopy133.dat' with(null '\r');
    4.null值是\r进行copy from
    copy t_copy_133 from '/xx/testcopy133.dat' with(null '\r');
    5.null值是\n进行copy to
    copy t_copy_133 to '/xx/testcopy133.dat' with(null '\n');
    6.null值是\n进行copy from
    copy t_copy_133 from '/xx/testcopy133.dat' with(null '\n');
    7.清理数据
    drop table t_copy_133;
    rm -rf /xx/testcopy133.dat
Expect      :
    1.创建测试表成功
    2.构造数据文件并写入内容成功
    3.copy失败
    4.copy失败
    5.copy失败
    6.copy失败
    7.清理数据
History     :
"""
import unittest
import os

from yat.test import Node
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant


class CopyFile(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_user = Node(node='PrimaryDbUser')
        self.Constant = Constant()
        self.common = Common()
        self.tb_name = 't_copy_133'
        self.file_name = 'testcopy133.dat'
        self.copy_dir_path = os.path.join(macro.DB_INSTANCE_PATH,
                                          'pg_copydir')
        self.error_msg = f'ERROR:  COPY null representation cannot ' \
            f'use newline or carriage return'

    def test_copy_file(self):
        text = '-----step1:创建测试表并对测试表插入数据' \
               'expect:创建测试表并插入数据成功-----'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name};"\
            f"create table {self.tb_name} (sk integer,id varchar(16)," \
            f"name varchar(20),create_timestamp timestamp);" \
            f"insert into {self.tb_name} values (001,'sk1','tt1','');" \
            f"insert into {self.tb_name} values (002,'sk2','tt2','');" \
            f"insert into {self.tb_name} values (003,'sk3','tt3','');"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, sql_res,
                      '执行失败:' + text)
        self.assertIn(self.Constant.INSERT_SUCCESS_MSG, sql_res,
                      '执行失败:' + text)

        text = '-----step2:构造数据文件;expect:构造数据文件成功-----'
        self.log.info(text)
        excute_cmd = f'''mkdir {self.copy_dir_path};
            touch {os.path.join(self.copy_dir_path, self.file_name)};'''
        self.log.info(excute_cmd)
        msg = self.common.get_sh_result(self.pri_user, excute_cmd)
        self.log.info(msg)
        ls_cmd = f'ls {self.copy_dir_path}'
        self.log.info(ls_cmd)
        msg = self.common.get_sh_result(self.pri_user, ls_cmd)
        self.log.info(msg)
        self.assertIn('testcopy133.dat', msg, '执行失败:' + text)

        text = '-----step3:null值是\r进行copy to;expect:copy失败-----'
        self.log.info(text)
        sql_cmd = f"copy {self.tb_name} to \
        '{os.path.join(self.copy_dir_path, self.file_name)}' with(null '\r');"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.error_msg, sql_res, '执行失败:' + text)

        text = '-----step4:null值是\r进行copy from;expect:copy失败-----'
        self.log.info(text)
        sql_cmd = f"copy {self.tb_name} from \
        '{os.path.join(self.copy_dir_path, self.file_name)}' with(null '\r');"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.error_msg, sql_res, '执行失败:' + text)

        text = '-----step5:null值是\n进行copy to;expect:copy失败-----'
        self.log.info(text)
        sql_cmd = f"copy {self.tb_name} to \
        '{os.path.join(self.copy_dir_path, self.file_name)}' with(null '\n');"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.error_msg, sql_res, '执行失败:' + text)

        text = '-----step6:null值是\n进行copy from;expect:copy失败-----'
        self.log.info(text)
        sql_cmd = f"copy {self.tb_name} from \
        '{os.path.join(self.copy_dir_path, self.file_name)}' with(null '\n');"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.error_msg, sql_res, '执行失败:' + text)

    def tearDown(self):
        text = '-----step7:清理环境;expect:清理环境成功-----'
        self.log.info(text)
        sql_cmd = self.pri_sh.execut_db_sql(
            f"drop table {self.tb_name};")
        self.log.info(sql_cmd)
        excute_cmd = f'''rm -rf {self.copy_dir_path}'''
        self.log.info(excute_cmd)
        msg = self.common.get_sh_result(self.pri_user, excute_cmd)
        self.log.info(msg)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, sql_cmd,
                      '执行失败:' + text)
        self.assertEqual(len(msg), 0, '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
